package cn.e4j.modular.api.service;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.mapper.Gwat161Mapper;
import cn.e4j.modular.api.util.CommonUtils;
import cn.e4j.modular.crm.entity.*;
import cn.e4j.modular.crm.mapper.*;
import cn.e4j.modular.system.entity.SysUser;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 *功能描述
 * @author caijy
 * @description
 * @datetime 2019-10-8 0008 14:13
 */

@Service
public class Gwat161Service {

    @Autowired
    Gwat161Mapper gwat161Mapper;

    @Autowired
    GwbkTMapper gwbkTMapper;

    @Autowired
    GwblTMapper gwblTMapper;

    @Autowired
    GwbeTMapper gwbeTMapper;

    @Autowired
    GwbhTMapper gwbhTMapper;

    @Autowired
    GwbaTMapper gwbaTMapper;

    @Autowired
    GwalTMapper gwalTMapper;

    @Autowired
    GwBiTMapper gwBiTMapper;

    @Autowired
    GwbjTMapper gwbjTMapper;


    /**
     * 拜访记录提报作业--获取拜访记录列表
     *
     * @param inDto
     * @return
     * @author wenchuan.li on 2018/06/14 10:33
     */
    public List<Dto> getGwat161List(Page<Object> page, Dto inDto) {
        return gwat161Mapper.getGwat161ListPage(page, inDto);
    }
//

    /**
     * 拜访记录--新增
     *
     * @param gwbh_tPO
     * @return
     * @author wenchuan.li on 2018/06/14 12:13
     */
    @Transactional
    public void gwat161Add(SysUser user,GwbhT gwbh_tPO, List<GwbkT> gwbk_tPOs, List<GwblT> gwbl_tPOs, List<GwbkT> gwbk_tPO1s, GwbaT gwba_tPO, GwbeT gwbe_tPO) {

        for (GwbkT gwbk_tPO : gwbk_tPOs) {
            gwbk_tPO.insert();
        }

        for (GwbkT gwbk_tPO : gwbk_tPO1s) {
            QueryWrapper<GwbkT> wrapper = new QueryWrapper<>();
            wrapper.eq("GWBKENT",gwbk_tPO.getGWBKENT());
            wrapper.eq("GWBKNAME",gwbk_tPO.getGWBKNAME());
            wrapper.eq("GWBKID",gwbk_tPO.getGWBKID());
            wrapper.eq("GWBKSEQ",gwbk_tPO.getGWBKSEQ());
            wrapper.eq("GWBKSEQ2",gwbk_tPO.getGWBKSEQ2());
            gwbk_tPO.update(wrapper);
        }

        GwblT gwblT = new GwblT();

        QueryWrapper<GwblT> wrapper = new QueryWrapper<>();
        wrapper.eq("GWBLENT",user.getDBC());
        wrapper.eq("GWBLNAME",gwbh_tPO.getGWBHNAME());
        wrapper.eq("GWBLID",gwbh_tPO.getGWBHID());
        gwblT.delete(wrapper);

        for (GwblT gwbl_tPO : gwbl_tPOs)
            gwbl_tPO.insert();

        QueryWrapper<GwbeT> gwbeTQueryWrapper = new QueryWrapper<>();
        gwbeTQueryWrapper.eq("GWBEENT",gwbe_tPO.getGWBEENT());
        gwbeTQueryWrapper.eq("GWBE001",gwbe_tPO.getGWBE001());
        gwbeTQueryWrapper.eq("GWBE002",gwbe_tPO.getGWBE002());
        gwbe_tPO.update(gwbeTQueryWrapper);
        gwbh_tPO.insert();
        gwba_tPO.insert();
    }
//

    /**
     * 拜访记录提报作业--获取拜访记录列表
     *
     * @param inDto
     * @return
     * @author wenchuan.li on 2018/07/4 9:33
     */
    public List<Dto> getBfrPage(Page page,Dto inDto) {
        return gwat161Mapper.getBfrPage01(page,inDto);
    }
//

    /**
     * 拜访记录提报作业--获取拜访记录列表
     * <p>
     * 拜访人 取客户资料表的数据
     *
     * @param inDto
     * @return
     * @author wenchuan.li on 2018/07/4 9:33
     */
    public List<Dto> getBfrPage01(Page page,Dto inDto) {
        List<Dto> bfrList = gwat161Mapper.getBfrPage01(page,inDto);
        List<Dto> list = new ArrayList<>();
        if (bfrList.size()>0){
            bfrList.forEach(bfr->{
                List<Dto> next = gwat161Mapper.getBfrPage01_next(bfr);
                if (CommonUtils.isNotEmpty(next))
                    list.add(next.get(0));
            });
        }
        return list;
    }
//

    /**
     * 获取拜访人的最大序列号
     *
     * @param inDto
     * @return
     * @author wenchuan.li on 2018/07/4 13:53
     */
    public List<Dto> getGwahMaxSeq(Dto inDto) {
        return gwat161Mapper.getGwahMaxSeq(inDto);
    }
//

    /**
     * 拜访人--新增
     *
     * @param gwbi_tPO
     * @return
     * @author wenchuan.li on 2018/07/04 14:21
     */
    @Transactional
    public void bfrAdd(GwbiT gwbi_tPO, GwalT gwal_tPO) {

        gwbi_tPO.insert();
        gwal_tPO.insert();

    }
//

    /**
     * 根据客户id获取 商机主题
     *
     * @param inDto
     * @return
     * @author wenchuan.li on 2018/07/11 13:44
     */
    public List<Dto> getSjztListByGwbg002(Dto inDto) {

        return gwat161Mapper.getSjztListByGwbg002(inDto);

    }
//

    /**
     * 根据主题+客户获取已有的 评估值
     *
     * @param inDto
     * @return
     * @author wenchuan.li on 2018/07/12 15:52
     */
    public List<Dto> getPgz(Dto inDto) {

        return gwat161Mapper.getPgz(inDto);

    }
//

    /**
     * 根据主题 获取已有的 竞争信息
     *
     * @param inDto
     * @return
     * @author wenchuan.li on 2018/07/12 17:13
     */
    public List<Dto> getJzxx(Dto inDto) {

        return gwat161Mapper.getJzxx(inDto);

    }
//

    /**
     * 根据主题+客户获取已有的 竞争信息最大项次
     *
     * @param inDto
     * @return
     * @author wenchuan.li on 2018/07/12 17:21
     */
    public List<Dto> getJzxxMax(Dto inDto) {
        return gwat161Mapper.getJzxxMax(inDto);
    }
//

    /**
     * 竞争信息--新增
     *
     * @param gwbj_tPO
     * @return
     * @author wenchuan.li on 2018/07/13 09:34
     */
    public void jzxxAdd(GwbjT gwbj_tPO) {

        gwbj_tPO.insert();

    }
//

    /**
     * 竞争信息--修改
     *
     * @param gwbj_tPO
     * @return
     * @author wenchuan.li on 2018/07/13 10:03
     */
    public void jzxxEdit(GwbjT gwbj_tPO) {
        UpdateWrapper<GwbjT> wrapper = new UpdateWrapper<>();
        wrapper.eq("GWBJENT",gwbj_tPO.getGWBJENT());
        wrapper.eq("GWBJNAME",gwbj_tPO.getGWBJNAME());
        wrapper.eq("GWBJID",gwbj_tPO.getGWBJID());
        wrapper.eq("GWBJSEQ",gwbj_tPO.getGWBJSEQ());
        gwbj_tPO.update(wrapper);
    }
//

    /**
     * 根据主题+客户获取商机拜访最大序号
     *
     * @param inDto
     * @return
     * @author wenchuan.li on 2018/07/16 16:20
     */
    public List<Dto> getGwbhseqMax(Dto inDto) {
        return gwat161Mapper.getGwbhseqMax(inDto);
    }
//
//    /**
//     * 根据主键删除关键评估项
//     *
//     * @param inDto
//     * @return
//     * @author wenchuan.li on 2018/07/16 16:25
//     */
//    public void deleteGwblByKey(Dto inDto) {
//        gwat161Mapper.deleteGwblByKey(inDto);
//    }
//

    /**
     * 根据主题+客户获取商机商品详情最大序号
     *
     * @param inDto
     * @return
     * @author wenchuan.li on 2018/07/16 16:20
     */
    public List<Dto> getGwbkseqMax(Dto inDto) {
        return gwat161Mapper.getGwbkseqMax(inDto);
    }
//

    /**
     * 根据主题+客户获取已有的 拜访记录最大批次
     *
     * @param inDto
     * @return
     * @author wenchuan.li on 2018/10/13 19:18
     */
    public List<Dto> getGwbkseq2Max(Dto inDto) {
        return gwat161Mapper.getGwbkseq2Max(inDto);
    }
//
//

    /**
     * 根据主题+客户获取已有的 商品明细
     *
     * @param inDto
     * @return
     * @author wenchuan.li on 2018/07/17 17:16
     */
    public List<Dto> gwat161Spmx(Dto inDto) {
        return gwat161Mapper.gwat161Spmx(inDto);
    }
//
//    /**
//     * 根据主键删除 商品明细
//     *
//     * @param inDto
//     * @return
//     * @author wenchuan.li on 2018/07/17 10:14
//     */
//    public void deleteGwbkByKey(Dto inDto) {
//        gwat161Mapper.deleteGwbkByKey(inDto);
//    }
//
//    /**
//     * 根据主键删除 商机主题所有 商品明细
//     *
//     * @param inDto
//     * @return
//     * @author wenchuan.li on 2018/07/17 10:14
//     */
//    public void deleteGwbkByKey2(Dto inDto) {
//        sqlDao.delete(Gwat161Service.class.getName() + ".deleteGwbkByKey2", inDto);
//    }
//
//

    /**
     * 拜访记录--修改
     *
     * @param gwbh_tPO
     * @return
     * @author wenchuan.li on 2018/07/18 11:34
     */
    @Transactional
    public void gwat161Edit(SysUser user, GwbhT gwbh_tPO, List<GwbkT> gwbk_tPOs, List<GwblT> gwbl_tPOs, List<GwbkT> gwbk_tPO1s, GwbeT gwbe_tPO, GwblT delGwblT) {
        //评估项
        if (delGwblT != null){
            //WHERE  GWBLENT = #{dto.GWBLENT} AND GWBLNAME = #{dto.GWBLNAME} AND GWBLID = #{dto.GWBLID}
            UpdateWrapper<GwblT> wrapper = new UpdateWrapper<>();
            wrapper.eq("GWBLENT",delGwblT.getGWBLENT());
            wrapper.eq("GWBLNAME",delGwblT.getGWBLNAME());
            wrapper.eq("GWBLID",delGwblT.getGWBLID());
            delGwblT.delete(wrapper);
        }

        for (GwbkT gwbk_tPO : gwbk_tPOs) {
            gwbk_tPO.insert();
        }

        for (GwbkT gwbk_tPO : gwbk_tPO1s) {
            //WHERE  GWBKENT = #{gwbkent} AND GWBKNAME = #{gwbkname} AND GWBKID = #{gwbkid} AND gwbkseq = #{gwbkseq} AND gwbkseq2 = #{GWBKSEQ2}
            UpdateWrapper<GwbkT> wrapper = new UpdateWrapper<>();
            wrapper.eq("GWBKENT",gwbk_tPO.getGWBKENT());
            wrapper.eq("GWBKNAME",gwbk_tPO.getGWBKNAME());
            wrapper.eq("GWBKID",gwbk_tPO.getGWBKID());
            wrapper.eq("GWBKSEQ",gwbk_tPO.getGWBKSEQ());
            wrapper.eq("GWBKSEQ2",gwbk_tPO.getGWBKSEQ2());
            gwbk_tPO.update(wrapper);
        }

        GwblT gwblT = new GwblT();
        //gwblT.setGWBLcurrentUser;
        gwblT.setGWBLENT(user.getDBC());
        gwblT.setGWBLNAME(gwbh_tPO.getGWBHNAME());
        gwblT.setGWBLID(gwbh_tPO.getGWBHID());

        UpdateWrapper<GwblT> wrapper = new UpdateWrapper<>();
        wrapper.eq("GWBLENT",gwblT.getGWBLENT());
        wrapper.eq("GWBLNAME",gwblT.getGWBLNAME());
        wrapper.eq("GWBLID",gwblT.getGWBLID());
        gwblT.delete(wrapper);

        for (GwblT gwbl_tPO : gwbl_tPOs) {
            gwbl_tPO.insert();
        }
        //WHERE  GWBHENT = #{gwbhent} AND GWBHNAME = #{gwbhname} AND GWBHID = #{gwbhid} AND GWBHSEQ = #{gwbhseq}
        UpdateWrapper<GwbhT> updateWrapper1 = new UpdateWrapper<>();
        updateWrapper1.eq("GWBHENT",gwbh_tPO.getGWBHENT());
        updateWrapper1.eq("GWBHNAME",gwbh_tPO.getGWBHNAME());
        updateWrapper1.eq("GWBHID",gwbh_tPO.getGWBHID());
        updateWrapper1.eq("GWBHSEQ",gwbh_tPO.getGWBHSEQ());
        gwbh_tPO.update(updateWrapper1);

        //WHERE  GWBEENT = #{gwbeent} AND GWBE001 = #{gwbe001} AND gwbe002 = #{gwbe002}
        UpdateWrapper<GwbeT> updateWrapper2 = new UpdateWrapper<>();
        updateWrapper2.eq("GWBEENT",gwbe_tPO.getGWBEENT());
        updateWrapper2.eq("GWBE001",gwbe_tPO.getGWBE001());
        updateWrapper2.eq("GWBE002",gwbe_tPO.getGWBE002());
        gwbe_tPO.update(updateWrapper2);

    }
//

    /**
     * 获取经销商信息
     *
     * @param inDto
     * @return
     */
    public List<Dto> getJxs(Dto inDto) {
        return gwat161Mapper.getJxs(inDto);
    }
//
//

    /**
     * 根据主题+客户获取已有的 评估值
     *
     * @param inDto
     * @return
     */
    public List<Dto> getPgx(Dto inDto) {

        return gwat161Mapper.getPgx(inDto);
    }
//

    /**
     * 删除拜访记录
     * <p>
     * 由于商品明细每次拜访的时候都会新增一份，所有需要删除
     *
     * @param inDto
     * @return
     * @update by wenchuan.li on 2018/10/13 16:18
     * @author wenchuan.li on 2018/08/28 11:07
     */
    @Transactional
    public void delBfjl(Dto inDto, int count) {
        String GWBHENT = inDto.getString("GWBHENT");
        String GWBHNAME = inDto.getString("GWBHNAME");
        String GWBHID = inDto.getString("GWBHID");
        String GWBHSEQ = inDto.getString("GWBHSEQ");

        QueryWrapper<GwbhT> wrapper = new QueryWrapper<>();
        wrapper.eq("GWBHENT",GWBHENT);
        wrapper.eq("GWBHNAME",GWBHNAME);
        wrapper.eq("GWBHID",GWBHID);
        wrapper.eq("GWBHSEQ",GWBHSEQ);
        new GwbhT().delete(wrapper);

//        gwat161Mapper.delBfjl(inDto);

        //根据主题删除 商品明细
        QueryWrapper<GwbkT> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("GWBKENT",GWBHENT);
        wrapper1.eq("GWBKNAME",GWBHNAME);
        wrapper1.eq("GWBKID",GWBHID);
        wrapper1.eq("GWBKSEQ2",GWBHSEQ);
        new GwbkT().delete(wrapper1);
//        gwat161Mapper.delGwbk(inDto);

        //根据主题删除 商品员工日历
//        gwat161Mapper.delGwba(inDto);
        QueryWrapper<GwbaT> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("GWBAENT",GWBHENT);
        wrapper2.eq("GWBA019",GWBHNAME);
        wrapper2.eq("GWBA015",GWBHID);
        wrapper2.eq("GWBA020",GWBHSEQ);
        new GwbaT().delete(wrapper2);

        if (count == 1) {
            //根据主题删除 关键评估项
//            gwat161Mapper.delGwbl(inDto);
            QueryWrapper<GwblT> wrapper3 = new QueryWrapper<>();
            wrapper3.eq("GWBLENT",GWBHENT);
            wrapper3.eq("GWBLNAME",GWBHNAME);
            wrapper3.eq("GWBLID",GWBHID);
            new GwblT().delete(wrapper3);

            //根据主题删除 竞争对手
//            gwat161Mapper.delGwbj(inDto);
            QueryWrapper<GwbjT> wrapper4 = new QueryWrapper<>();
            wrapper4.eq("GWBJENT",GWBHENT);
            wrapper4.eq("GWBJNAME",GWBHNAME);
            wrapper4.eq("GWBJID",GWBHID);
            new GwbjT().delete(wrapper4);

            //根据主题删除 拜访角色
//            gwat161Mapper.delGwbi(inDto);
            QueryWrapper<GwbiT> wrapper5 = new QueryWrapper<>();
            wrapper5.eq("GWBIENT",GWBHENT);
            wrapper5.eq("GWBINAME",GWBHNAME);
            wrapper5.eq("GWBIID",GWBHID);
            new GwbiT().delete(wrapper5);
        }
    }
//

    /**
     * 根据主题获取 拜访记录的条目
     *
     * @param inDto
     * @return
     * @author wenchuan.li on 2018/08/28 11:36
     */
    public List<Dto> countBfjl(Dto inDto) {
        return gwat161Mapper.countBfjl(inDto);
    }
//

    /**
     * 获取客户列表
     * <p>
     * 1.最终客户、2.经销商、3.内部法人、4.其他类
     *
     * @param inDto
     * @return
     * @author wenchuan.li on 2018/07/10 13:53
     */
    public List<Dto> getGwakList(Page<Object> page, Dto inDto) {
        return gwat161Mapper.getGwakPage(page, inDto);
    }


    /**
     * 获取拜访记录（商机主题、客户编号）
     *
     * @param inDto
     * @return
     * @author wenchuan.li on 2018/09/03 10:11
     */
    public List<Dto> getGwbhListByGwbhname(Dto inDto) {
        return gwat161Mapper.getGwbhListByGwbhname(inDto);
    }


    /**
     * 根据所属组织获取拜访内容填写分类
     *
     * @param inDto
     * @return
     * @author wenchuan.li on 2018/09/27 16:58
     */
    public List<Dto> getGwbfListByzz(Dto inDto) {
        return gwat161Mapper.getGwbfListByzz(inDto);
    }
//
}
